import 'package:example/edited_data_controller.dart';
import 'package:example/video_editor/video_editor_view_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class TimeSlider extends StatefulWidget {
  const TimeSlider({super.key});

  @override
  _TimeSliderState createState() => _TimeSliderState();
}

class _TimeSliderState extends State<TimeSlider> {
  String _formatDuration(Duration duration) {
    String minutes = (duration.inMinutes % 60).toString().padLeft(2, '0');
    String seconds = (duration.inSeconds % 60 +
            ((duration.inMilliseconds % 1000) * 0.001).round())
        .toString()
        .padLeft(2, '0');
    return '$minutes:$seconds';
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VideoEditorViewModel>(
      id: 'timer',
      builder: (vm) {
        var duration = vm.allEditedData!.duration;
        var allDuration =
            Duration(milliseconds: (vm.allEditedData!.duration * 1000).round());

        return Obx(() => Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      _formatDuration(
                          Duration(milliseconds: vm.position.value)),
                      style: const TextStyle(fontSize: 20, color: Colors.white),
                    ),
                    Text(
                      _formatDuration(allDuration),
                      style: const TextStyle(fontSize: 20, color: Colors.white),
                    ),
                  ],
                ),
                Slider(
                  value: vm.position.value / 1000,
                  min: 0.0,
                  max: duration + 0.1,
                  onChanged: (newValue) {
                    vm.setPosition((newValue * 1000).toInt());
                  },
                ),
              ],
            ));
      },
    );
  }
}
